package com.zzyk.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.zzyk.main.model.pojo.Device;
import com.zzyk.main.model.pojo.DeviceParam;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.DeviceOriginDataService;
import com.zzyk.main.service.DeviceParamService;
import com.zzyk.main.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wss_PC
 * @DESCRIPTION 历史数据
 * @create 2020-7-8
 */
@RestController
@RequestMapping("/historyData")
public class HistoryDataController {
    @Autowired
    private DeviceOriginDataService dataService;
    @Resource
    private DeviceService deviceService;

    @Resource
    private DeviceParamService deviceParamService;

    /**
     * 获取历史数据
     *
     * @param eId
     * @param dId
     * @param param
     * @return
     */
    @GetMapping("/getHistoryDataByParam")
    public Map<String, Object> getHistoryDataByParam(Integer eId, Integer dId, String param, String beginDate,
                                                     String endDate, Integer type) {
        Map map = new HashMap(2);
        map.put("data", dataService.findDeviceOriginData(eId, dId, param, beginDate, endDate, type));
        map.put("param", param);
        return map;
    }

    /**
     * 获取历史数据
     *
     * @param eId
     * @param dId
     * @param param
     * @return
     */
    @GetMapping("/getHistoryDataByParamTest")
    public Message getHistoryDataByParamTest(Integer eId, Integer dId, String param, String beginDate,
                                             String endDate, Integer type) {
        Integer isHarmonic = 0;
        Integer isSimplex = 0;
        Device device = deviceService.selectVwDeviceProduceById(dId);
        if (ObjectUtil.isNotNull(device)) {
            DeviceParam deviceParam = deviceParamService.selectOneByDeviceId(device.getClientId());
            if (ObjectUtil.isNotNull(deviceParam) && ObjectUtil.isNotNull(deviceParam.getHarmonic())) {
                isHarmonic = deviceParam.getHarmonic();
                if (ObjectUtil.isNotNull(deviceParam.getPowerSupply())) {
                    isSimplex = deviceParam.getPowerSupply();
                }
            }
        }
        return Message.success()
                .addData("data", dataService.findDeviceOriginDataTest(eId, dId, param, beginDate, endDate, type))
                .addData("isSimplex", isSimplex)
                .addData("isHarmonic", isHarmonic);
    }
}
